//
//  HomeTableViewController.swift
//  sinaWeiBo
//
//  Created by smartlei on 15/10/14.
//  Copyright © 2015年 smartlei. All rights reserved.
//

import UIKit

let statusNormalCellID = "statusNormalCellID"
let statusRetweetedCellID = "statusRetweetedCellID"

class HomeTableViewController: VisitorTableViewController {

    //建造VM模型
   private var dataListVM = StatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !UserAccountViewModel.sharedUserAccountViewModel.userLogon {
            //用户没有登录
            visitorView?.setInfo(nil, msg: "关注一些人，回这里看看有什么惊喜")
            
            return
        }
        //设置无线条样式
        tableView.separatorStyle = .None
//        //注册cell
        prepareTableViewCell()
//        //加载数据
        loadData()
//        //使用预估方法自动调整cell的高度
        tableView.estimatedRowHeight = 200
       // tableView.rowHeight = 400        
    }
    //注册cell
    private func prepareTableViewCell() {
        
        //注册cell
        tableView.registerClass(StatusRetweetedCell.self, forCellReuseIdentifier: statusRetweetedCellID)
        //注册cell
        tableView.registerClass(StatusNormalCell.self, forCellReuseIdentifier: statusNormalCellID)
        
    }
    //加载数据
    private func loadData() {
    
        dataListVM.loadData { (isSuccessed) -> () in
            
            if !isSuccessed {
                
                return
            }
            //加载数据正确刷新数据
            self.tableView.reloadData()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
//MARK - UITableViewDataSource数据源方法
extension HomeTableViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataListVM.statusListViewModel.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let statusViewModel = dataListVM.statusListViewModel[indexPath.row]
        //let cellId = statusViewModel.cellID
        
        let  cell = tableView.dequeueReusableCellWithIdentifier(statusViewModel.cellID, forIndexPath: indexPath) as! StatusCell
        
     
        
        cell.statusViewModel = statusViewModel
        
        return cell
    }
}
//MARK - TableViewController代理方法
extension HomeTableViewController {
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        let statusViewModel = dataListVM.statusListViewModel[indexPath.row]
        
        //print(statusViewModel.rowheight)
        //每次取得时候计算
        return statusViewModel.rowheight
    }
  
    
    
}



